package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;


    @Autowired
    private Executor threadPoolTaskExecutor;

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 根据专辑ID查询专辑详情
     *
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getItemData(Long albumId) {
        //0.判断访问专辑在布隆器是否存在
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean contains = bloomFilter.contains(albumId);
        //true:说明可能存在
        if (!contains) {
            throw new GuiguException(404, "专辑不存在");
        }
        //1.创建封装专辑详情相关数据Map 存在多线程并发写，选择线程安全集合类HashTable,ConcurrentHashMap
        Map<String, Object> map = new ConcurrentHashMap<>();
        //2.远程调用专辑服务获取专辑信息-封装专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑:{}不存在", albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolTaskExecutor).exceptionally(e -> {
            log.error("查询专辑详情失败:{}", e.getMessage());
            return null;
        }).orTimeout(1, TimeUnit.SECONDS);

        //3.远程调用专辑服务获取分类信息-封装分类信息
        CompletableFuture<Void> categorViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(categoryView, "专辑分类:{}不存在", albumInfo.getCategory3Id());
            map.put("baseCategoryView", categoryView);
        }, threadPoolTaskExecutor);

        //4.远程调用专辑服务获取统计信息-封统计信息
        CompletableFuture<Void> albumStatCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计:{}不存在", albumId);
            map.put("albumStatVo", albumStatVo);
        }, threadPoolTaskExecutor);

        //5.远程调用用户服务获取主播信息-封主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "主播:{}不存在", albumInfo.getUserId());
            map.put("announcer", userInfoVo);
        }, threadPoolTaskExecutor);

        //6.通过异步任务对象组合任务  所有异步任务都必须执行完成
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                albumStatCompletableFuture,
                categorViewCompletableFuture,
                announcerCompletableFuture
        ).join();
        return map;
    }
}
